package com.av.android;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Utils {

	public static final String URL_PREFIX = "http://titan.cmpe.boun.edu.tr:8085/cmpe451Web/";
	public static String httpGet(String operation){


		String responseString = null;
		try {
			HttpClient httpclient = new DefaultHttpClient();
			System.out.println(URL_PREFIX+operation);
			HttpResponse response = httpclient
					.execute(new HttpGet(URL_PREFIX+operation));
			StatusLine statusLine = response.getStatusLine();
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				response.getEntity().writeTo(out);
				out.close();
				responseString = out.toString();
				Log.d("asd", responseString);
				// ..more logic
			} else {
				Log.d("asd", statusLine.getReasonPhrase());
				response.getEntity().getContent().close();
				throw new IOException(statusLine.getReasonPhrase());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseString;
	}
	public static String register(String name,String surname,String username, String password, String email){
		String operation = "AndroidRegister?name="+URLEncoder.encode(name)
				+"&surname="+URLEncoder.encode(surname)
				+"&email="+URLEncoder.encode(email)
				+"&password="+URLEncoder.encode(password)
				+"&username="+URLEncoder.encode(username);
		return httpGet(operation);

	}
	public static List<Report> searchReports(String selectedTag,
			String selectedDisability, String selectedStatus) {

		String operation = "AndroidSearch?obstacleType="+URLEncoder.encode(selectedDisability)
				+"&reportStatus="+URLEncoder.encode(selectedStatus)
				+"&tag="+URLEncoder.encode(selectedTag);
		String responseString = httpGet(operation);
		ObjectMapper mapper = new ObjectMapper();
		List<Report> reports=null;
		try {
			reports = mapper.readValue(responseString,
					new TypeReference<List<Report>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return reports;
	}
	public static List<Report> getOwnReports(int user_id) {
		String operation = "AndroidOwnReports?userID="+user_id;
		String responseString = httpGet(operation);
		ObjectMapper mapper = new ObjectMapper();
		List<Report> reports=null;
		try {
			reports = mapper.readValue(responseString,
					new TypeReference<List<Report>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return reports;

	}
	public static List<Report> getSubscriptions(int user_id) {
		String operation = "AndroidSubscriptions?userID="+user_id;
		String responseString = httpGet(operation);
		ObjectMapper mapper = new ObjectMapper();
		List<Report> reports=null;
		try {
			reports = mapper.readValue(responseString,
					new TypeReference<List<Report>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return reports;
	}
	public static User getUser(long reportID){
		String operation = "AndroidGetUserById?reportID="+reportID;
		String responseString = httpGet(operation);
		ObjectMapper mapper = new ObjectMapper();
		User user = null;
		try {
			user = mapper.readValue(responseString,User.class);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;

	}
	public static List<ObstacleType> GetObstacles(){
		String operation ="GetObstacles";
		String responseString = httpGet(operation);
		List<ObstacleType> obstacles=null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			obstacles = mapper.readValue(responseString,
					new TypeReference<List<ObstacleType>>() {
			});

		} catch (Exception e) {
			Log.e("asd", e.toString());
		}
		return obstacles;
	}
	public static List<Tag> getTags(){
		String operation ="GetTags";
		String responseString = httpGet(operation);
		List<Tag> tags=null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			tags = mapper.readValue(responseString,
					new TypeReference<List<Tag>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return tags;
	}
	public static List<String> getTagsOfReport(long reportID){
		String operation ="GetTagsOfReport?reportID="+reportID;
		String responseString = httpGet(operation);
		List<String> tags=null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			tags = mapper.readValue(responseString,
					new TypeReference<List<String>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return tags;
	}
	public static List<Comment> getComments(long report_id) {
		// TODO Auto-generated method stub
		String operation ="GetComments?reportID="+report_id;
		String responseString = httpGet(operation);
		List<Comment> comments=null;
		ObjectMapper mapper = new ObjectMapper();
		try {
			comments= mapper.readValue(responseString,
					new TypeReference<List<Comment>>() {
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		return comments;
	}
	public static String upOrDownRate(String updown,long reportID,long userID){
		String operation ="AndroidRate?operation="+updown+"&userID="+userID+"&reportID="+reportID;
		return httpGet(operation);


	}
	public static String subscribe(long reportID,long userID){
		String operation ="AndroidSubscribe?userID="+userID+"&reportID="+reportID;
		return httpGet(operation);


	}
	public static boolean isSubscribed(long userID,long reportID){
		String operation ="AndroidIsSubscribed?reportID="+reportID+"&userID="+userID;
		String responseString = httpGet(operation);
		if(responseString.equals("true"))
			return true;
		else
			return false;
	}
	public static String saveComment(String comment,long reportID,long userID) {
		// TODO Auto-generated method stub
		String operation ="AndroidComment?reportID="+reportID+"&userID="+userID+"&comment="+URLEncoder.encode(comment);
		String responseString = httpGet(operation);
		return responseString;
	}
	public static Bitmap getBitmapFromUrl(String url){
		try {

			BitmapFactory.Options options=new BitmapFactory.Options();
			options.inSampleSize = 4;
			System.out.println(URL_PREFIX+"image/"+ url);
			final Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(URL_PREFIX+"image/"+ url).getContent(),null,options);
			return bitmap;
		} catch (MalformedURLException e) {
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		

	}

}
